<?php


namespace App\Models\CacheModel\zl1_relation;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl1IdeaTag extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_idea_tag';

    protected $cacheModel = 'relation_analysis.tag';

    /**
     * 获取 我的理想型 标签
     * @param array $field
     * @return array
     * @throws \Exception
     */
    public function getTags(array $field = [])
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setSort('DESC')->ZsGetIocData('list', function ()
        {
            try
            {
                $data = $this->where(['status' => 1])->order('sort desc')->column('sort', 'id');
            }
            catch (\Exception $e)
            {
                $data = [];
            }
            return $data;
        }, true);

        $data = $this->batchGetInfoByIds(array_keys($ids), $field);

        $tName = [
            1 => '外貌',
            2 => '性格',
            3 => '表达方式',
            4 => '事业特征',
            5 => '财富来源',
            6 => '感情经历',
            7 => '人际交往',
        ];
        $resData = [];
        foreach ($ids as $id => $s)
        {
            $d = $data[$id];
            if (isset($resData[$d['type']]))
            {
                $resData[$d['type']]['list'][] = [
                    'id' => $id,
                    'tname' => $d['name'],
                    'sex' => $d['sex'],
                ];
            } else
            {
                $resData[$d['type']] = [
                    'id' => $d['type'],
                    'type' => $d['sex'] === 0 ? 2 : 1,
                    'title' => $tName[$d['type']],
                    'list' => [
                        [
                            'id' => $id,
                            'tname' => $d['name'],
                            'sex' => $d['sex'],
                        ]
                    ]
                ];
            }
        }

        asort($resData);

        return array_values($resData);
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        $field = 'type,status,name,sort,idea_type,sex';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'type', 'sort', 'status', 'idea_type', 'sex']);
        }

        return $resData;
    }
}
